﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : PhotonServerProject | PhotonServerFramework | ServerConfiguration.cs
// //  Created : 2016 - 03 - 21 12:43
// //  Modified: 2016 - 04 - 18 09:37
// //  ********************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using ExitGames.Logging;
using NHibernate;
using PhotonServerCommon.Entities.Domain;
using PhotonServerFramework.Implementation.Database;
using PhotonServerFramework.Interfaces.Configuration;

namespace PhotonServerFramework.Implementation.Configuration
{
    public class ServerConfiguration : IServerConfiguration
    {
        private ILogger Logger { get; }

        public int ClientUdpPort { get; set; }
        public int ConnectionRetryInterval { get; set; }
        public string IpAddress { get; set; }
        public bool IsMasterServer { get; set; }
        public string LocalIpAddress { get; set; }
        public int LocalPort { get; set; }
        public string MasterIpAddress { get; set; }
        public int MasterTcpPort { get; set; }
        public int MasterUdpPort { get; set; }
        public int MaximumRetries { get; set; }
        public string Name { get; set; }
        public string Application { get; set; }
        public int ServerTcpPort { get; set; }

        public ServerConfiguration(string application, ILogger logger)
        {
            Logger = logger;
            Application = application;

            Logger.InfoFormat("Trying to resolve configuration from database for application {0}", application);

            using (ISession session = SessionManager.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    try
                    {
                        IList<ParameterT> parameters =
                            session.QueryOver<ParameterT>()
                                .Where(x => x.ParCompGrp == "Server")
                                .And(x => x.ParComp == application)
                                .List<ParameterT>();

                        Logger.InfoFormat("Found {0} parameters for application {1}", parameters.Count, application);

                        PropertyInfo[] properties = GetType().GetProperties();

                        foreach (PropertyInfo property in properties)
                        {
                            string propValue = parameters.First(c => c.ParKey == property.Name).ParValue;
                            Type propType = property.PropertyType;
                            Logger.InfoFormat("Setting property {0} to value {1}", property.Name, propValue);
                            property.SetValue(this, Convert.ChangeType(propValue, propType), null);
                        }

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorFormat("Operation Failed with: {0}", ex.Message);
                        transaction.Rollback();
                    }
                }

                session.Close();
            }
        }
    }
}
